package com.netmi.myjectpack.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.netmi.myjectpack.R;
import com.netmi.myjectpack.bean.UserInfo;
import com.netmi.myjectpack.databinding.FragmentDetailsLivedataBinding;
import com.netmi.myjectpack.viewmodel.MyBaseLiveDataViewModel;

import java.util.Random;

/**
 * 类描述：
 * 创建人：tgw
 * 创建时间：2020/8/6
 * 修改备注：
 */
public class LiveDataDetailsFragment extends Fragment {
    public static final String TAG = LiveDataDetailsFragment.class.getName();

    private FragmentDetailsLivedataBinding mBinding;
    private MyBaseLiveDataViewModel model;

    private UserInfo info;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mBinding == null)
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_details_livedata, container, false);
        return mBinding.getRoot();
    }


    //使用databin绑定点击事件
    public void doClick(View view) {
        Random random = new Random();
        info.setNickname("tgw详情改变" + random.nextInt(100));

        model.getUserInfo().setValue(info);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        info = new UserInfo();
        info.setNickname("tgw详情");
        // Get the ViewModel.注意 这里使用 getActivity() 引用绑定activity，这里fragment改变userinfo数据
        //  LiveDataFragment中的内容也会相应改变，使用ViewModelProvider(this)则不会，代表和另一个不是绑定同一个引用实例
        model = new ViewModelProvider(getActivity()).get(MyBaseLiveDataViewModel.class);

//        mBinding.setLifecycleOwner(this);//这句话 功能类似于下面这个方法，就是更新userinfo数据


        model.getUserInfo().observe(getViewLifecycleOwner(), new Observer<UserInfo>() {
            @Override
            public void onChanged(UserInfo userInfo) {
                mBinding.tvName.setText(userInfo.getNickname());
            }
        });

        //这个方法和 下面的 model.getUserInfo().observe(this, nameObserver); 结合起来
        // 就相当于上面一个方法
//        final Observer<UserInfo> nameObserver = new Observer<UserInfo>() {
//            @Override
//            public void onChanged(@Nullable final UserInfo userInfo) {
//                // Update the UI, in this case, a TextView.
//                mBinding.tvName.setText(userInfo.getNickname());
//                userInfo.setNickname("tgw灵台一中 哈哈");
//                mBinding.tvName.setText(userInfo.getNickname());
//            }
//        };

        // Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
//        model.getUserInfo().observe(this, nameObserver);

        //设置初始值
        mBinding.setLivedataDetailsviewmodel(model);//如果上面的onChanged方法没有赋值 这里就要使用databind将值放到xml里面赋值
        //databinding的绑定事件
        mBinding.setLiveDataFragment(LiveDataDetailsFragment.this);
    }
}
